package com.neu.dao.impl;

import com.neu.dao.BaseDao;
import com.neu.dao.BussinessDao;

import java.util.List;
import java.util.Map;

public class BussinessDaoImpl extends BaseDao implements BussinessDao {
    @Override
    //商家登录
    public Map<String,Object> login(String name,String pwd){
        Map<String,Object> map=null;
        String sql="SELECT * FROM tbl_bussiness\n" +
                "WHERE bussiness_name = ? AND bussiness_pwd = md5(?)";
        List<Map<String,Object>> list = super.executeQuery(sql,name,pwd);
        if (list.size()>0){
            map=list.get(0);
        }
        return map;
    }

    @Override
    //查询所有商家
    public List<Map<String, Object>> findAll() {
        List<Map<String,Object>> list = null;
        String sql ="SELECT bussiness_id,bussiness_name,bussiness_address,bussiness_startprice,bussiness_deliveryprice,\n" +
                "case \n" +
                "\twhen bussiness_status=1 then '营业中'\n" +
                "\telse '已注销'\n" +
                "end bussiness_status from tbl_bussiness";
        list=super.executeQuery(sql);
        return list;
    }

    @Override
    //根据名称查询商家信息
    public List<Map<String, Object>> findByName(String name) {
        List<Map<String,Object>> list = null;
        String sql ="SELECT bussiness_id,bussiness_name,bussiness_address,bussiness_startprice,bussiness_deliveryprice,\n" +
                "case \n" +
                "\twhen bussiness_status=1 then '营业中'\n" +
                "\telse '已注销'\n" +
                "end bussiness_status from tbl_bussiness WHERE bussiness_name LIKE ?";
        list=super.executeQuery(sql,"%"+name+"%");
        return list;
    }

    @Override
    //管理员添加商家
    public int addBussiness(String pwd, String name, String address,
                            String explain, String startprice, String deliveryprice) {
        int row = 0;
        String sql="INSERT into tbl_bussiness(bussiness_pwd,bussiness_name,bussiness_address,bussiness_explain,bussiness_startprice,bussiness_deliveryprice)\n" +
                "VALUES(MD5(?),?,?,?,?,?)";
        row = executeUpdate(sql,pwd,name,address,explain,startprice,deliveryprice);
        return row;
    }

    @Override
    //修改密码
    public int updatePwd(String newPwd, String oldPwd, String id) {
        int row = 0;
        String sql="update tbl_bussiness set bussiness_pwd=md5(?)" +
                " where bussiness_id = ? and bussiness_pwd = md5(?)";
        row = executeUpdate(sql,newPwd,id,oldPwd);
        return row;
    }

    @Override
    //修改商家信息
    public int updateBussiness(String addr, String info, String startPirce, String deliveryprice, String id) {
        int row = 0;
        String sql = "UPDATE tbl_bussiness\n" +
                "SET bussiness_address = ?,\n" +
                "bussiness_explain =?,\n" +
                "bussiness_startprice = ?,\n" +
                "bussiness_deliveryprice = ?,\n" +
                "WHERE bussiness_id = ?";
        row = super.executeUpdate(sql,addr,info,startPirce,deliveryprice,id);
        return row;
    }

    @Override
    public Map<String, Object> findById(String id) {
        List<Map<String,Object>> list = null;
        Map<String,Object> map =null;
        String sql = "SELECT bussiness_id,bussiness_name,bussiness_address,bussiness_startprice,bussiness_deliveryprice,\n" +
                "case \n" +
                "\twhen bussiness_status=1 then '在'\n" +
                "\telse '删除'\n" +
                "end bussiness_status from tbl_bussiness" +
                " where bussiness_id = ?";
        list= super.executeQuery(sql,id);
        if (list.size()>0){
            map = list.get(0);
        }
        return map;
    }

    @Override
    public int deleteBussiness(String id) {
        int row = 0;
        String sql = "UPDATE tbl_bussiness\n" +
                "SET bussiness_status= 0\n" +
                "WHERE bussiness_id =?";
        row = super.executeUpdate(sql,id);
        return row;
    }
}
